<?php include 'inc/teacher_head.php';?>



<div class="layui-container">




    <div  id="div_bjkc">
        <div class="layui-form layui-form-pane">
            <!-- 插入选择班级、课程的组件 -->
            <?php [$showTitle, $showClass, $showTask]  = [ 1, 1, 1]; ?>
            <?php include 'inc/widget_bjkc.php'; ?>
        </div>
    </div>




<!-- 如果没有班级、课程，则不显示下面表格 -->
<?php if (!empty(@GS('classes')) && !empty(@GS('tasks'))) {?>
    <!-- 如果已有设置，则显示表格 -->
    <div class="layui-form-item" style="width:80%;text-align:center;margin:0 auto;">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 40px;">
            <legend> 当前机房设定如下 </legend>
        </fieldset>

        <table class="layui-table" id="tb_setup" lay-filter="tb_setup"></table>

    </div>
<?php }?>



<div class="layui-row" style="width:80%;text-align:center;margin:0 auto;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
            <legend>

                <div style="font-size:16pt;">记事本</div>
                <div id="saveHint" style='font-size:9px;color:red;float: left;'>(默认自动保存,手动保存请按Ctrl+S)</div>

            </legend>
        </fieldset>
    <div class="">
      <textarea id='notepad' class='layui-textarea' style="background-color: #eee;height: 200px;" placeholder="用途：可以记录各班教学进度等"><?=GS('auth')['notepad'];?></textarea>
    </div>

</div>



</div>




<script id="tpl_room" type="text/html">
    {{d.room}}
</script>
<script id="tpl1" type="text/html">
    <input  type="checkbox" lay-filter="game_onoff" name="game_onoff"
        lay-skin="switch" lay-text="开启|关闭" {{d.allow_game==='1'?'checked':null}}>
</script>
<script id="tpl2" type="text/html">
    <input type="checkbox" lay-filter="register_onoff" name="register_onoff"
        lay-skin="switch" lay-text="允许|禁止" {{d.allow_register==='1'?'checked':null}}>
</script>

<script language="javascript">

    layui.use(['form', 'element', 'table'], function () {
            var form = layui.form
                , table = layui.table
                , $ = layui.$;
            $("#nav_setup").addClass('layui-this'); //设置导航当前选中项的样式

//监听按键 Ctrl+S保存
$(document).ready(function () {
            $(document).keydown(function (e) {
                if (e.ctrlKey && e.key === 's') {
                    e.preventDefault(); // 阻止浏览器默认的保存操作
                    saveTxt();
                }
            });
        });

    //保存记事本
    function saveTxt(){
        let text=$("#notepad").val();
        $.post('/teacher/notepad', {text:text}, function (r) {
            var time = new Date().toLocaleTimeString();
            $('#saveHint').text("记事本保存成功! 最近保存时间于 "+time);
        });
    };

    //页面跳转、关闭、刷新 保存记事本
    window.onbeforeunload=function(){
        saveTxt();
    };


    //游戏中心开关
    form.on('switch(game_onoff)', function () {
        var allow = this.checked ? 'true' : 'false';
        $.post('/teacher/allowGame', {allow: allow});
    });
    //学生注册开关
    form.on('switch(register_onoff)', function () {
        var allow = this.checked ? 'true' : 'false';
        $.post('/teacher/allowRegister', {allow: allow});
    });

    //选择班级 选择课程
    form.on('select()', function(){
        let sel_task  = $('select[name="tasks"]').find("option:selected");
        let sel_class = $('select[name="classes"]').find("option:selected");
       let data={};
        data.class_id       = sel_class.val();
        data.category_id    = sel_task.attr('category_id');
        data.task_id        = sel_task.val();
        console.log(data);
        //当班级和课程都选了后，确认设置
        if (data.class_id && data.category_id && data.task_id){
            $.post('/teacher/Setup', data, function () {
                table.reload('setupTable');
                layer.msg('设定成功');
            });
        }
        return false;
    });


    // 显示本机房的设置
    table.render({
        elem: '#tb_setup'
        , id: 'setupTable'
        , url:'getSetup'
        , cols: [[
             {templet: '#tpl_room', width: 100,  align: 'center',  title: '地 点'}
             // {field: 'room', width: 100,  align: 'center',  title: '地 点'}
            , {field: 'class_name', width: 100,  align: 'center', title: '班 级'}
            , {field: 'realname', width: 100,  align: 'center',  title: '教 师'}
            , {field: 'category_name',  align: 'center',  title: '课 程 分 类'}
            , {field: 'task_name',  align: 'center',  title: '课 程 名 称'}
            , {templet: '#tpl1', minWidth: 110,  align: 'center',  title: '游戏中心'}
            , {templet: '#tpl2', minWidth: 110,  align: 'center',  title: '学生注册'}
        ]]
        ,page:false
        ,done:function(res){
            if (res.code==1){ // 无设置时
                layer.alert('请先设置上课班级和本节课程!',{icon:0});
            }

        }
    });



});
</script>

<?php include 'inc/teacher_foot.php';?>